<template>
  <div class="home">
    <my-header>
      <template #left>
        <span>编辑</span>
      </template>
      <template #center> 购物车 </template>
      <template #right>
        <span @click="show = !show">筛选</span>
      </template>
    </my-header>
    <div class="content">
      <my-cart
        v-for="(item, index) in goodsList"
        :key="index"
        :itemObj="item"
        @childFtn="childFtn"
      ></my-cart>
    </div>
    <div class="foot">
      <span
        ><input
          type="checkbox"
          v-model="allCheck"
          @change="checkFtn"
        />全选</span
      >
      <span>总价：{{ allPrice }}</span>
      <span>去支付：{{ allNum }}</span>
    </div>
    <transition name="a">
      <div class="right" v-show="show">
        <h4>关键字搜索</h4>
        <p>
          <span
            v-for="(item, index) in searchList"
            :key="index"
            :class="{ active: searchTxt == item }"
            @click="searchTxt = searchTxt == item ? '' : item"
            >{{ item }}</span
          >
        </p>
        <section>
          <button @click="show = !show">取消</button>
          <button @click="sure">确定</button>
        </section>
      </div>
    </transition>
  </div>
</template>

<script>
import MyHeader from "../components/myHeader.vue";
// @ is an alias to /src
import api from "../api/index";
import { reactive, toRefs, computed } from "vue";
import MyCart from "../components/myCart.vue";
export default {
  name: "Home",
  components: {
    MyHeader,
    MyCart,
  },
  setup(props) {
    const data = reactive({
      show: false,
      searchList: ["海信", "海尔", "山水", "美的"],
      searchTxt: "",
      goodsList: [],
      allCheck: false,
      allPrice: computed(() => {
        let price = 0;
        data.goodsList.forEach((item) => {
          if (item.checked) {
            price += item.num * item.price;
          }
        });
        return price;
      }),
      allNum: computed(() => {
        let num = 0;
        data.goodsList.forEach((item) => {
          if (item.checked) {
            num += item.num;
          }
        });
        return num;
      }),
    });
    api.get("data.json").then((res) => {
      data.goodsList = res.cartData;
    });
    const checkFtn = () => {
      // 全选的函数
      data.goodsList.forEach((item) => {
        item.checked = data.allCheck;
      });
    };
    const childFtn = () => {
      // 反选
      data.allCheck = data.goodsList.every((item) => item.checked);
    };
    const sure = () => {
      // 确定搜索的函数
      api.get("data.json").then((res) => {
        data.goodsList = res.cartData.filter((item) =>
          item.name.includes(data.searchTxt)
        );
      });
      data.show = false;
    };
    return {
      ...toRefs(data),
      checkFtn,
      childFtn,
      sure,
    };
  },
};
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
  .content {
    height: calc(100% - 100px);
    overflow: auto;
  }
  .foot {
    display: flex;
    height: 50px;
    align-items: center;
    background: red;
    color: #fff;
    font-size: 14px;
    justify-content: space-around;
  }
}
.right {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 99;
  background: #fff;
  padding: 20px;
  box-sizing: border-box;
  width: 70%;
  height: 100%;
  h4 {
    line-height: 50px;
  }
  span {
    padding: 3px 6px;
    margin-left: 10px;
  }
  .active {
    color: red;
  }
  section {
    margin-top: 10px;
    display: flex;
    justify-content: space-around;
  }
}
.a-enter-active {
  transition: all 0.3s linear;
}

.a-leave-active {
  transition: all 0.3s linear;
}

.a-enter-from,
.a-leave-to {
  transform: translateX(100%);
  opacity: 0;
}
</style>
